#include "ProductTestUartRead.h"

Com_s32 g_rec_len = 0;
Com_s32 g_data_len = DEF_HEAD_LENGTH;
Com_s32 g_rec_head_flag = 0;

int Ext_InPerUartRead_Process(unsigned char *buff,Com_s32 serial_fd)
{
    int i;
    int retval;
    int nread = -1;
    fd_set rfds;
    struct timeval tv;
    static unsigned char rec_b[DEF_PER_UART_BUF_SIZE]={0};
    unsigned char temp_r = 0;

    tv.tv_sec = 0;			//set the rcv wait time
    tv.tv_usec = (DEF_PER_UART_READ_MS * 1000);	//100000us = 0.1s

    FD_ZERO(&rfds);
    FD_SET(serial_fd,&rfds);
    retval = select(serial_fd + 1, &rfds,NULL,NULL,&tv);
    usleep(1000);
    if(retval == -1)
    {
        perror("select()");
    }
    else if(retval)
    {
        if (read(serial_fd, &temp_r, 1) > 0)
        {
            if (g_rec_head_flag == 1)
            {
                rec_b[g_rec_len] = temp_r;
                g_rec_len++;

                g_data_len = DEF_HEAD_LENGTH + ((rec_b[4] << 8) + rec_b[5]) + 1;
                if(g_rec_len >= g_data_len)
                {
                    if (g_data_len > DEF_PER_UART_BUF_SIZE)
                    {
                        g_rec_len = 0;
                        g_rec_head_flag = 0;
                        memset(rec_b, 0, sizeof(rec_b));
                        printf("receive error data length ----> \n");
                        return nread;
                    }

                    memcpy(buff, rec_b, g_data_len);
                    PRT_NORMAL("\nnread=%d ---> ", g_data_len);

                    for (i = 0; i < g_data_len; i++)
                    {
                        PRT_NORMAL("0x%02x ", buff[i]);
                    }
                    PRT_NORMAL("\n");

                    g_rec_head_flag = 0;
                    g_rec_len = 0;
                    int temp = g_data_len;
                    memset(rec_b, 0, sizeof(rec_b));

                    return temp;
                }
            }
            else
            {
                if ((rec_b[0] == DEF_HEAD_FIRST) && (g_rec_len == 1))
                {
                    switch (temp_r)
                    {
                    case DEF_HEAD_SECOND:
                        rec_b[1] = temp_r;
                        g_rec_head_flag = 1;
                        g_rec_len = 2;
                        break;

                    default:
                        rec_b[0] = 0;
                        g_rec_len = 0;
                        break;
                    }
                }
                else
                {
                    if ((temp_r == DEF_HEAD_FIRST) && (g_rec_len == 0))
                    {
                        rec_b[0] = temp_r;
                        g_rec_len = 1;
                    }
                    else
                    {
                        rec_b[0] = 0;
                        g_rec_len = 0;
                    }
                }
            }
        }
    }
    else if (retval == 0)
    {
        //DBG("serial get data timeout !\n");
        return 0;
    }

    return nread;
}
